package top.v5it.japi.plus.sdk;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * http工具
 *
 * @author zhanpu
 * @date 2021/3/9
 */
@Slf4j
public class JapiHttpUtil {

    /**
     * disabled contractor
     */
    private JapiHttpUtil() {
        throw new UnsupportedOperationException();
    }

    /**
     * 请求调用
     *
     * @param method    {@link Method}
     * @param url       请求地址
     * @param headerMap 头信息
     * @param prefix    响应的head前缀
     * @param timeout   超时时间
     * @param body      请求参数json格式
     * @return {@link JapiReturnVo}
     */
//    public static JapiReturnVo request(Method method, String url, Map<String, String> headerMap, String prefix, int timeout, String... body) {
//        //创建请求对象
//        HttpRequest request =
//                HttpUtil.createRequest(method, url)
//                        .headerMap(headerMap, true)
//                        .timeout(timeout)
//                        .body(Stream.of(body).allMatch(StrUtil::isBlank) ? null : body[0]);
//
//        //执行调用并同步获取结果
//        try (HttpResponse response = request.execute()) {
//            String timestamp = response.header(prefix + "-Timestamp");
//            String nonce = response.header(prefix + "-Nonce");
//            String serial = response.header(prefix + "-Serial");
//            String signature = response.header(prefix + "-Signature");
//            String id = response.header("Request-ID");
//
//            //封装结果并返回
//            return new JapiReturnVo()
//                    .setTimestamp(timestamp)
//                    .setNonce(nonce)
//                    .setSerial(serial)
//                    .setSignature(signature)
//                    .setRequestId(id)
//                    .setResult(response.body())
//                    .setStatus(response.getStatus());
//        } catch (Exception e) {
//            log.error(e.getMessage(), e);
//            //失败封装异常返回
//            throw new CallJapiServerException("请求第三方服务错误", e);
//        }
//    }

    public static Function<String, JapiReturnVo> request(Method method, String url, Map<String, String> headerMap, int timeout, String... body) {
        // 创建请求对象
        HttpRequest request =
                HttpUtil.createRequest(method, url)
                        .headerMap(headerMap, true)
                        .timeout(timeout)
                        .body(Stream.of(body).allMatch(StrUtil::isBlank) ? null : body[0]);

        return prefix -> {

            // 执行调用并同步获取结果
            try (HttpResponse response = request.execute()) {
                String timestamp = response.header(prefix + "-Timestamp");
                String nonce = response.header(prefix + "-Nonce");
                String serial = response.header(prefix + "-Serial");
                String signature = response.header(prefix + "-Signature");
                String id = response.header("Request-ID");

                //封装结果并返回
                return new JapiReturnVo()
                        .setTimestamp(timestamp)
                        .setNonce(nonce)
                        .setSerial(serial)
                        .setSignature(signature)
                        .setRequestId(id)
                        .setResult(response.body())
                        .setStatus(response.getStatus());
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                //失败封装异常返回
                throw new CallJapiServerException("请求第三方服务错误", e);
            }
        };
    }
}
